package com.tyk.bookstore.back.platform.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.common.core.util.IdGenerator;
import com.tyk.bookstore.back.common.core.util.ObjUtil;
import com.tyk.bookstore.back.platform.model.entity.SystemSettingDo;
import com.tyk.bookstore.back.platform.model.query.SystemSettingAddQuery;
import com.tyk.bookstore.back.platform.model.query.SystemSettingPageQuery;
import com.tyk.bookstore.back.platform.model.query.SystemSettingUpdateQuery;
import com.tyk.bookstore.back.platform.model.result.SystemSettingResultEnum;
import com.tyk.bookstore.back.platform.model.vo.SystemSettingPageVo;
import com.tyk.bookstore.back.platform.model.vo.SystemSettingVo;
import com.tyk.bookstore.back.platform.repository.SystemSettingRepository;
import com.tyk.bookstore.back.platform.service.SystemSettingService;
import com.tyk.bookstore.back.platform.util.MapStructPlatform;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Set;


/**
 * @author tyk
 * @description 系统设置
 */
@Service
@RequiredArgsConstructor

public class SystemSettingServiceImpl extends ServiceImpl<SystemSettingRepository, SystemSettingDo> implements SystemSettingService {

    private final SystemSettingRepository systemSettingRepository;
    private final MapStructPlatform mapStructPlatform;

    @Override
    public SystemSettingVo getById(Long id) {
        SystemSettingDo systemSetting = systemSettingRepository.selectById(id);
        AssertUtil.notNull(systemSetting, SystemSettingResultEnum.SETTING_NOT_FOUND);
        return mapStructPlatform.toSystemSettingVo(systemSetting);
    }

    @Override
    public SystemSettingPageVo getPage(SystemSettingPageQuery query) {
        Page<SystemSettingDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<SystemSettingDo> w = buildPageQueryWrapper(query);
        systemSettingRepository.selectPage(page, w);
        return mapStructPlatform.toSystemSettingPageVo(page);
    }


    @Override
    public void add(SystemSettingAddQuery query) {
        // 变量名不重复
        AssertUtil.isFalse(
                systemSettingRepository.exist1Eq(
                        SystemSettingDo::getName, query.getName(), SystemSettingDo.class
                ),
                SystemSettingResultEnum.SETTING_ALREADY_EXISTS
        );
        // 添加
        SystemSettingDo systemSetting = mapStructPlatform.toSystemSettingDo(query);
        systemSetting.setId(IdGenerator.id());
        systemSettingRepository.insert(systemSetting);
    }

    @Override
    public void updateById(SystemSettingUpdateQuery query) {
        // 变量名不重复
        AssertUtil.isFalse(
                systemSettingRepository.exist1Eq1Ne(
                        SystemSettingDo::getName, query.getName(),
                        SystemSettingDo::getId, query.getId(),
                        SystemSettingDo.class
                ),
                SystemSettingResultEnum.SETTING_ALREADY_EXISTS
        );
        // 更新
        SystemSettingDo systemSetting = mapStructPlatform.toSystemSettingDo(query);
        systemSettingRepository.updateById(systemSetting);
    }

    @Override
    public void deleteById(Long id) {
        systemSettingRepository.deleteById(id);
    }

    @Override
    public void deleteBatchByIdSet(Set<Long> idSet) {
        systemSettingRepository.deleteBatchIds(idSet);
    }


    /**
     * 构建分页查询Wrapper
     */
    private LambdaQueryWrapper<SystemSettingDo> buildPageQueryWrapper(SystemSettingPageQuery query) {

        LambdaQueryWrapper<SystemSettingDo> w = new LambdaQueryWrapper<>();

        w.like(ObjUtil.notBlank(query.getKeyword()), SystemSettingDo::getDescription, query.getKeyword());

        w.like(ObjUtil.notBlank(query.getName()), SystemSettingDo::getName, query.getName());

        w.like(ObjUtil.notBlank(query.getValue()), SystemSettingDo::getValue, query.getValue());

        w.ge(query.getCreateTimeFrom() != null, SystemSettingDo::getCreateTime, query.getCreateTimeFrom());
        w.le(query.getCreateTimeTo() != null, SystemSettingDo::getCreateTime, query.getCreateTimeTo());

        w.ge(query.getUpdateTimeFrom() != null, SystemSettingDo::getUpdateTime, query.getUpdateTimeFrom());
        w.le(query.getUpdateTimeTo() != null, SystemSettingDo::getUpdateTime, query.getUpdateTimeTo());

        w.orderBy(true, false, SystemSettingDo::getCreateTime);

        return w;
    }

}

